package javafx;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;

//定义DrawListener事件处理类，该类继承自MouseListener鼠标事件接口，重写接口中的抽象方法。
public class DrawListener implements MouseListener,MouseMotionListener,ActionListener {

    private int x1,x2,y1,y2;
    //定义Graphics画笔类的对象属性名
    private Graphics2D g;
    public String name = "直线";
    private Color color = Color.red;
    private JFrame frame;

    private Paint[] array;//定义存储图形的数组
    private int index = 0;

    public DrawListener(JFrame frame,Paint[] array) {
        this.frame = frame;
        this.array = array;
    }

    //定义一个带Graphics参数的构造方法
    public void setG(Graphics g) {
        this.g = (Graphics2D) g;
        this.g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);// 设置画笔开启抗锯齿
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println("点击的按钮是："+e.getActionCommand());
        if(e.getActionCommand().equals("")) {
            JButton button = (JButton) e.getSource();//获取事件源对象
            color = button.getBackground();//获取背景颜色
        }else {
            name=e.getActionCommand();//获取按钮信息
        }


    }

    public void mouseDragged(MouseEvent e){
        //System.out.println("拖动");
        x2 = e.getX();
        y2 = e.getY();
        switch(name) {
            case "铅笔":
                g.setStroke(new BasicStroke(2));//设置线条的粗细
                g.drawLine(x1, y1, x2, y2);//画曲线
                Paint paint = new Paint(x1,y1,x2,y2,color,name,2);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = paint;//将数组对象存入到数组中
                x1 = x2;
                y1 = y2;
                break;
            case "刷子":
                g.setStroke(new BasicStroke(10));//设置线条的粗细
                g.drawLine(x1, y1, x2, y2);//画曲线
                Paint pain = new Paint(x1,y1,x2,y2,color,name,10);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = pain;//将数组对象存入到数组中
                x1 = x2;
                y1 = y2;
                break;
            case "橡皮":
                color = frame.getContentPane().getBackground();
                g.setColor(color);
                g.setStroke(new BasicStroke(50));
                g.drawLine(x1, y1, x2, y2);//画曲线
                Paint pai = new Paint(x1,y1,x2,y2,color,name,50);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = pai;//将数组对象存入到数组中
                x1 = x2;
                y1 = y2;
                break;
            case "喷枪":
                Random rand = new Random();
                for(int i=0;i<10;i++) {
                    int p = rand.nextInt(10);
                    int q = rand.nextInt(10);
                    g.drawLine(x2+p, y2+q, x2+p, y2+q);

                    Paint pa = new Paint(x2+p, y2+q, x2+p, y2+q,color,name);//根据图形的数据实例化Paint对象
                    if(index<1000)
                        array[index++] = pa;//将数组对象存入到数组中
                }
                x1 = x2;
                y1 = y2;
                break;
        }
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
        //System.out.println("点击");
    }

    public void mousePressed(MouseEvent e) {
        //System.out.println("按下");
        //在按下和释放的事件处理方法中获取按下和释放的坐标值
        x1 = e.getX();
        y1 = e.getY();
        g.setColor(color);
    }

    public void mouseReleased(MouseEvent e) {
        //System.out.println("释放");
        x2 = e.getX();
        y2 = e.getY();

        //设置画笔的颜色
        //g.setColor(Color.green);
        //g.setColor(new Color(100, 100, 100));
        //根据按下和释放的坐标值，使用Graphics对象进行画图

        g.setStroke(new BasicStroke(1));//设置线条的粗细
        switch(name) {
            case "直线":
                g.drawLine(x1, y1, x2, y2);
                Paint paint = new Paint(x1,y1,x2,y2,color,name,1);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = paint;//将图形对象存入到数组中
                break;

            case "矩形":
                g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                Paint k = new Paint(x1,y1,x2,y2,color,name,1);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = k;//将图形对象存入到数组中
                break;

            case "圆":
                g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                Paint f = new Paint(x1,y1,x2,y2,color,name,1);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = f;//将图形对象存入到数组中
                break;

            case "文字":
                g.drawString("这是文字效果", x1, y1);
                Paint j = new Paint(x1,y1,x2,y2,color,name,1);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = j;//将图形对象存入到数组中
                break;

            case "长方体":
                g.setColor(new Color(100, 200, 100));
                g.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2),Math.abs(y1-y2));
                int a,b,c,d;
                a=Math.min(x1, x2);
                b=Math.max(x1, x2);
                c=Math.min(y1, y2);
                d=Math.max(y1, y2);


                int m=(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4));
                int n=(int)((b-a)*Math.cos(Math.PI/4)*Math.sin(Math.PI/4));
                //顶面
                g.setColor(Color.green);
                g.fillPolygon(new int[] {a, a+m, b+m,b},new int[] {c,c-n,c-n,c},4);
                //右侧面
                g.setColor(Color.black);
                g.fillPolygon(new int[] {b, b, b+m,b+m},new int[] {c,d,d-n,c-n},4);
                Paint h = new Paint(x1,y1,x2,y2,color,name,1);//根据图形的数据实例化Paint对象
                if(index<1000)
                    array[index++] = h;//将图形对象存入到数组中
                break;
        }
    }

    public void mouseEntered(MouseEvent e) {
        //System.out.println("进入");
    }

    public void mouseExited(MouseEvent e) {
        //System.out.println("离开");
    }
}
